package com.basejace.system.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.basejace.common.model.PubDepartment;
import com.basejace.common.model.PubRole;
import com.basejace.common.model.PubUser;
import com.basejace.common.utils.MD5;
import com.basejace.core.R;
import com.basejace.core.model.LoginUserBean;
import com.basejace.core.web.SessionUtils;
import com.jfinal.core.Controller;
import com.jfinal.kit.PropKit;
import com.jfinal.log.Log;

public class LoginController extends Controller {
	protected Log log = Log.getLog(this.getClass());
	public void index() {
		
		if (this.getRequest().getSession().getAttribute(R.Key.LOGIN_USER) != null)
			redirect("/main");
		else 
			render("/login.jsp");
	}
	
	public void login() {
		String userName = getPara("username");
		String password = getPara("password");
		/*
		try {
			PubUser user = PubUser.dao.findFirst("select * from pub_user where username=?", StringUtils.trim(userName));
			if (user == null) {
				setAttr(R.Const.MSG, "用户名或密码不正确");
				render("/login.jsp");
				return;
			}
			if ( !MD5.encode(StringUtils.trimToEmpty(password)).equals(user.getPassword())){
				setAttr(R.Const.MSG, "用户名或密码不正确");
				render("/login.jsp");
				return;
			}
			SessionUtils.me().setLoginUser(this.getSession(), new LoginUserBean(user));
			//填写登录日志库表
			if (PropKit.getBoolean("loginlog", false)) {
				
			}
		} catch (Exception e) {
			e.printStackTrace();
			setAttr(R.Const.MSG, "系统错误："+e.getMessage());
			render("/login.jsp");
			return;
		}*/
		redirect("/main");
	}
	
	public void main() {
		//获取角色 和 部门
//		String findRoleSql = "select a.* from pub_role a , pub_userrole_rela b where a.userid=b.userid and a.userid=?";
//		String findDepartmentSql = "select a.* from pub_department a , pub_userdepartment_rela b where a.userid=b.userid and a.userid=?";
//		
//		List<PubRole> roleList = PubRole.dao.find(findRoleSql, SessionUtils.me().getUserid());
//		List<PubDepartment> dptList = PubDepartment.dao.find(findDepartmentSql, SessionUtils.me().getUserid());
		//获取权限菜单列表
		
		
		
		
		//设置当前用户默认首页控制台
		setAttr("indexPage", "index.jsp");
		
		render("/page/main.jsp");
	}
	
}
